package com.jianjian.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.jianjian.gulimall.product.entity.BrandEntity;
import com.jianjian.gulimall.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jianjian.gulimall.product.entity.CategoryBrandRelationEntity;
import com.jianjian.gulimall.product.service.CategoryBrandRelationService;
import com.jianjian.common.utils.PageUtils;
import com.jianjian.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author cool
 * @email cool@gmail.com
 * @date 2022-08-27 15:47:16
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private CategoryBrandRelationService brandRelationService;

    /**
     * /product/categorybrandrelation/brands/list
     *获取分类关联的品牌
     *
     * 1.controller: 处理请求，接收和校验数据
     * 2.service接收controller传来的数据，进行业务处理
     * 3.controller接收处理完的数据，封装页面指定的vo
     *
     */
    @GetMapping("/brands/list")
    public R brandsList(@RequestParam("catId") Long catId){
       List<BrandEntity> brandEntities = brandRelationService.selectlistBrandByCatId(catId);
       List<CategoryBrandRelationVo> list =brandEntities.stream().map(item->{
           CategoryBrandRelationVo brandRelationVo = new CategoryBrandRelationVo();
          brandRelationVo.setBrandId(item.getBrandId());
          brandRelationVo.setBrandName(item.getName());
           return brandRelationVo;
       }).collect(Collectors.toList());

        return R.ok().put("data",list);
    }

    /**
     * 获取当前品牌所有关联分类的列表
     */
    @RequestMapping(value = "catelog/list" , method = RequestMethod.GET)
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R cateloglist(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)
        );

        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
